import {
  BANNER_API,
  RECOMMEND_LIST_API,
  RECOMMEND_LIST_DETAIL_API,
} from "../../api/url";
import http from "../../api/http";
export default {
  namespaced: true,
  state: {
    banner: [],
    recommend: [],
    bannerLoading: false,
    recommendLoading: false,
    recommendListDetail: {},
    detailLoading: false, // 歌单详情页面的加载
  },
  getters: {
    loading(state) {
      //推荐首页的加载
      return state.bannerLoading || state.recommendLoading;
    },
  },
  mutations: {
    // 设置轮播图状态
    setBannerLoading(state, payload) {
      state.bannerLoading = payload;
    },
    setBanner(state, payload) {
      state.banner = payload;
    },
    // 推荐歌单状态
    setRecommendLoading(state, payload) {
      state.recommendLoading = payload;
    },
    setRecommend(state, payload) {
      state.recommend = payload;
    },
    // 设置推荐歌单详情
    setDecommendListDetail(state, payload) {
      state.recommendListDetail = payload;
    },
    setDetailLoading(state, payload) {
      state.detailLoading = payload;
    },
  },
  actions: {
    // 请求轮播图的数据
    async requestBannerList(context) {
      context.commit("setBannerLoading", true);
      const {
        data: { banners },
      } = await http.get(BANNER_API);
      const newData = banners.map((item) => item.imageUrl);
      context.commit("setBanner", newData);
      context.commit("setBannerLoading", false);
    },
    // 请求推荐歌单
    async requestRecommendList(context) {
      context.commit("setRecommendLoading", true);
      const {
        data: { result },
      } = await http.get(RECOMMEND_LIST_API);
      const newData = result.map(({ id, name, picUrl, playCount }) => ({
        id,
        name,
        picUrl,
        playCount,
      }));
      context.commit("setRecommend", newData);
      context.commit("setRecommendLoading", false);
    },
    // 请求推荐歌单列表详情
    async requestRecommendListDetail(context, payload) {
      context.commit("setDetailLoading", true);
      const {
        data: { playlist },
      } = await http.get(RECOMMEND_LIST_DETAIL_API, payload);
      console.log(playlist);
      const listDetail = {
        // 歌单名字
        name: playlist.name,
        // 歌单图片
        coverImgUrl: playlist.coverImgUrl,
        // 歌单收藏数量
        subscribedCount: playlist.subscribedCount,
        // 创建者信息
        creator: {
          nickname: playlist.creator.nickname,
          avatarUrl: playlist.creator.avatarUrl,
        },
        // 歌单歌曲列表
        tracks: playlist.tracks.map((item) => ({
          id: item.id, //id
          title: item.name, //歌名
          ar: item.ar.map((arItem) => arItem.name), //歌手
          al: {
            //专辑
            name: item.al.name,
            picUrl: item.al.picUrl,
          },
        })),
      };
      context.commit("setDecommendListDetail", listDetail);
      context.commit("setDetailLoading", false);
    },
  },
};
